import Vue from 'vue'

export default {
  namespaced: true,
  state: {
    // 记录用户答案
    userAnswerMap: {},
    // 特殊标记
    markMap: {},
    // 回显答案
    showAnswer: false,
    // 记录已通过题目
    passedQuestionMap: {}
  },
  mutations: {
    SET_USER_ANSWER_MAP(state, data) {
      let { uid, answer } = data
      let isEmpty =
        (Array.isArray(answer) && !answer.length) ||
        (typeof answer === 'string' && !answer.length)
      if (isEmpty) {
        Vue.delete(state.userAnswerMap, uid)
      } else {
        Vue.set(state.userAnswerMap, uid, answer)
      }
    },

    SET_USER_ANSWER_MAP_ALL(state, data) {
      if (!data) return
      Vue.set(state, 'userAnswerMap', {})
      Object.keys(data).forEach(key => {
        Vue.set(state.userAnswerMap, key, data[key])
      })
    },

    SET_MARK_MAP(state, data) {
      let { uid, value } = data
      if (value) {
        Vue.set(state.markMap, uid, 1)
      } else {
        Vue.delete(state.markMap, uid)
      }
    },
    SET_SHOW_ANSWER(state, data) {
      state.showAnswer = data
    },
    SET_PASSED_QUESTION_MAP_ALL(state, data) {
      if (!data) return
      Object.keys(data).forEach(key => {
        Vue.set(state.passedQuestionMap, key, data[key])
      })
    }
  },
  actions: {},
  getters: {
    getUserMap: state => {
      return state.userAnswerMap
    }
  }
}
